//
//  RecordViewController.m
//  GuoBuJiaForIphone
//
//  Created by zhangwo on 16/11/4.
//  Copyright © 2016年 GuoBuJia. All rights reserved.
//

#import "RecordViewController.h"

@interface RecordViewController ()

@end

@implementation RecordViewController
@synthesize signArray = _signArray;
@synthesize topLine = _topLine;
@synthesize buttonLine = _buttonLine;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"打卡记录"];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.leftBarButtonItem = [DSXUI barButtonWithStyle:DSXBarButtonStyleBack target:self action:@selector(back)];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    _topLine = [UIView new];
    _buttonLine = [UIView new];
    _buttonLine.backgroundColor = [UIColor colorWithHexString:@"0xD8D8D8"];
    _topLine.backgroundColor = [UIColor colorWithHexString:@"0xD8D8D8"];
    [_tableView addSubview:_topLine];
    [_tableView addSubview:_buttonLine];
    
    [_tableView registerClass:[RemarkTableViewCell class] forCellReuseIdentifier:@"aCell"];
    
    [[DSXHttpManager sharedManager] POST:@"&c=sign&a=showlist" parameters:@{@"uid":@([BJUserStatus sharedStatus].uid)} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            _signArray = [responseObject objectForKey:@"data"];
            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

- (void)back{
    if (![self.navigationController popViewControllerAnimated:YES]) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _signArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    NSMutableDictionary *signData = [_signArray objectAtIndex:indexPath.row];
    RemarkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"aCell" forIndexPath:indexPath];
    }
    cell.dataList = signData;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //使创建的 topLine 视图背景颜色 等于 cell中 topLine 的背景颜色
    _topLine.backgroundColor = cell.topLine.backgroundColor;
    _buttonLine.backgroundColor = cell.buttomLine.backgroundColor;
    //获取cell中topLine 或者bottomLine 的 x 位置。
    /**
     *   将像素point由point所在视图转换到目标视图view中，返回在目标视图view中的像素值
     *   (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
     */
    _leadingSpaceOfLines =  [cell convertPoint:cell.topLine.frame.origin toView:self.view].x;
    _tableView.separatorStyle = NO;//隐藏tableViewCell的线条
    
    [self scrollViewDidScroll:tableView];
    return cell;
}

#pragma mark --important code--
//根据上下拉动，动态改变 topLine 和 bottomLine 的 y 轴坐标。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _topLine.frame = CGRectMake(_leadingSpaceOfLines, 0, 1, -scrollView.contentOffset.y);
    
    CGFloat yOffSet = scrollView.frame.size.height - scrollView.contentSize.height + scrollView.contentOffset.y ;
    _buttonLine.frame = CGRectMake(_leadingSpaceOfLines, self.view.frame.size.height - yOffSet, 1, self.view.frame.size.height);
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
